home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows 95 / Programming Windows 95.iso / code / CHAP14 / BIGJOB1.C next >
Encoding:
C/C++ Source or Header  |  1996-01-01  |  4.5 KB  |  159 lines

  1. /*----------------------------------------
  2.    BIGJOB1.C -- Multithreading Demo
  3.                 (c) Charles Petzold, 1996
  4.   ----------------------------------------*/
  5.  
  6. #include <windows.h>
  7. #include <math.h>
  8. #include <process.h>
  9.  
  10. #define REP              100000
  11.  
  12. #define STATUS_READY     0
  13. #define STATUS_WORKING   1
  14. #define STATUS_DONE      2
  15.  
  16. #define WM_CALC_DONE     (WM_USER + 0)
  17. #define WM_CALC_ABORTED  (WM_USER + 1)
  18.  
  19. typedef struct
  20.      {
  21.      HWND hwnd ;
  22.      BOOL bContinue ;
  23.      }
  24.      PARAMS, *PPARAMS ;
  25.  
  26. LRESULT APIENTRY WndProc (HWND, UINT, WPARAM, LPARAM) ;
  27.  
  28. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  29.                     PSTR szCmdLine, int iCmdShow)
  30.      {
  31.      static char szAppName[] = "BigJob1" ;
  32.      HWND        hwnd ;
  33.      MSG         msg ;
  34.      WNDCLASSEX  wndclass ;
  35.  
  36.      wndclass.cbSize        = sizeof (wndclass) ;
  37.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  38.      wndclass.lpfnWndProc   = WndProc ;
  39.      wndclass.cbClsExtra    = 0 ;
  40.      wndclass.cbWndExtra    = 0 ;
  41.      wndclass.hInstance     = hInstance ;
  42.      wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  43.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  44.      wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
  45.      wndclass.lpszMenuName  = NULL ;
  46.      wndclass.lpszClassName = szAppName ;
  47.      wndclass.hIconSm       = LoadIcon (NULL, IDI_APPLICATION) ;
  48.  
  49.      RegisterClassEx (&wndclass) ;
  50.  
  51.      hwnd = CreateWindow (szAppName, "Multithreading Demo",
  52.                           WS_OVERLAPPEDWINDOW,
  53.                           CW_USEDEFAULT, CW_USEDEFAULT,
  54.                           CW_USEDEFAULT, CW_USEDEFAULT,
  55.                           NULL, NULL, hInstance, NULL) ;
  56.  
  57.      ShowWindow (hwnd, iCmdShow) ;
  58.      UpdateWindow (hwnd) ;
  59.  
  60.      while (GetMessage (&msg, NULL, 0, 0))
  61.           {
  62.           TranslateMessage (&msg) ;
  63.           DispatchMessage (&msg) ;
  64.           }
  65.      return msg.wParam ;
  66.      }
  67.  
  68. void Thread (PVOID pvoid)
  69.      {
  70.      double  A = 1.0 ;
  71.      INT     i ;
  72.      LONG    lTime ;
  73.      PPARAMS pparams ;
  74.  
  75.      pparams = (PPARAMS) pvoid ;
  76.  
  77.      lTime = GetCurrentTime () ;
  78.  
  79.      for (i = 0 ; i < REP && pparams->bContinue ; i++)
  80.           A = tan (atan (exp (log (sqrt (A * A))))) + 1.0 ;
  81.  
  82.      if (i == REP)
  83.           {
  84.           lTime = GetCurrentTime () - lTime ;
  85.           SendMessage (pparams->hwnd, WM_CALC_DONE, 0, lTime) ;
  86.           }
  87.      else
  88.           SendMessage (pparams->hwnd, WM_CALC_ABORTED, 0, 0) ;
  89.  
  90.      _endthread () ;
  91.      }
  92.  
  93. LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
  94.      {
  95.      static char  *szMessage[] = { "Ready (left mouse button begins)",
  96.                                    "Working (right mouse button aborts)",
  97.                                    "%d repetitions in %ld msec" } ;
  98.      static INT    iStatus ;
  99.      static LONG   lTime ;
  100.      static PARAMS params ;
  101.      char          szBuffer[64] ;
  102.      HDC           hdc ;
  103.      PAINTSTRUCT   ps ;
  104.      RECT          rect ;
  105.  
  106.      switch (iMsg)
  107.           {
  108.           case WM_LBUTTONDOWN :
  109.                if (iStatus == STATUS_WORKING)
  110.                     {
  111.                     MessageBeep (0) ;
  112.                     return 0 ;
  113.                     }
  114.  
  115.                iStatus = STATUS_WORKING ;
  116.  
  117.                params.hwnd = hwnd ;
  118.                params.bContinue = TRUE ;
  119.  
  120.                _beginthread (Thread, 0, ¶ms) ;
  121.  
  122.                InvalidateRect (hwnd, NULL, TRUE) ;
  123.                return 0 ;
  124.  
  125.           case WM_RBUTTONDOWN :
  126.                params.bContinue = FALSE ;
  127.                return 0 ;
  128.  
  129.           case WM_CALC_DONE :
  130.                lTime = lParam ;
  131.                iStatus = STATUS_DONE ;
  132.                InvalidateRect (hwnd, NULL, TRUE) ;
  133.                return 0 ;
  134.  
  135.           case WM_CALC_ABORTED :
  136.                iStatus = STATUS_READY ;
  137.                InvalidateRect (hwnd, NULL, TRUE) ;
  138.                return 0 ;
  139.  
  140.           case WM_PAINT :
  141.                hdc = BeginPaint (hwnd, &ps) ;
  142.  
  143.                GetClientRect (hwnd, &rect) ;
  144.  
  145.                wsprintf (szBuffer, szMessage[iStatus], REP, lTime) ;
  146.  
  147.                DrawText (hdc, szBuffer, -1, &rect,
  148.                          DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;
  149.  
  150.                EndPaint (hwnd, &ps) ;
  151.                return 0 ;
  152.  
  153.           case WM_DESTROY :
  154.                PostQuitMessage (0) ;
  155.                return 0 ;
  156.           }
  157.      return DefWindowProc (hwnd, iMsg, wParam, lParam) ;
  158.      }
  159.